package com.lulalei.dbviewer.commons.factory.GenerateBeanFactory;

import com.lulalei.dbviewer.commons.bean.JDBCConnectBean;
import com.lulalei.dbviewer.commons.bean.TableBean;
import com.lulalei.dbviewer.commons.cache.CacheManager;
import com.lulalei.dbviewer.commons.cache.CacheMap;
import com.lulalei.dbviewer.commons.enums.DatabaseTypeEnums;
import com.lulalei.dbviewer.commons.exception.CustomizeException;
import com.lulalei.dbviewer.db.DBService;
import com.lulalei.dbviewer.db.mysql.DTO.TableAndColumnsQueryDTO;
import com.lulalei.dbviewer.dto.GenerateDTO;
import com.lulalei.dbviewer.strategy.DBStrategyManager;
import lombok.RequiredArgsConstructor;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@Component
@RequiredArgsConstructor
public abstract class AbstractGenerateBeanFactory implements GenerateBeanFactory {

    private final DBStrategyManager dbManager;

    /**
     * 获取处理表字段信息的服务类
     */
    public DBService getService(String connection) throws CustomizeException {
        CacheMap jdbc = CacheManager.getCacheMap("jdbc");
        JDBCConnectBean connectBean = (JDBCConnectBean) jdbc.get(connection);
        DatabaseTypeEnums dbType = connectBean.getDbInfo().getDbType();
        return dbManager.getDBService(dbType.name());
    }


    List<TableBean> getTableAndField(GenerateDTO generateDTO) throws CustomizeException, SQLException {
        //获取表及表字段信息
        DBService service = getService(generateDTO.getConnectionName());
        TableAndColumnsQueryDTO queryDTO = new TableAndColumnsQueryDTO();
        queryDTO.setConnectionName(generateDTO.getConnectionName());
        queryDTO.setTableNames(generateDTO.getTableNames());
        return service.getTableAndField(queryDTO);
    }

    public abstract String moduleQualifiedName(String moduleName) throws XmlPullParserException, IOException, CustomizeException;

}
